package com.pilot.game.service;

import com.pilot.game.domain.Customer;
import com.pilot.game.repository.CustomerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Service Implementation for managing Customer.
 */
@Service
@Transactional
public class CustomerService {

    private final Logger log = LoggerFactory.getLogger(CustomerService.class);

    private final CustomerRepository customerRepository;

    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    /**
     * Save a customer.
     *
     * @param customer the entity to save
     * @return the persisted entity
     */
    public Customer save(Customer customer) {
        log.debug("Request to save Customer : {}", customer);
        return customerRepository.save(customer);
    }

    /**
     * Get all the customers.
     *
     * @param example
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<Customer> findAll(Example<Customer> example, Pageable pageable) {
        log.debug("Request to get all Customers example :{}", example);
        return customerRepository.findAll(example, pageable);
    }

    /**
     * Get one customer by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public Customer findOne(Long id) {
        log.debug("Request to get Customer : {}", id);
        return customerRepository.findOne(id);
    }

    /**
     * Delete the  customer by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Customer : {}", id);
        customerRepository.delete(id);
    }

    /**
     * findByNameAndPwd
     *
     * @param accountName accountName
     * @param password    password
     * @return entity
     */
    public Customer findByNameAndPwd(String accountName, String password) {
        log.debug("Request to findByNameAndPwd Customer : {},{}", accountName, password);
        return customerRepository.findByAccountNameAndPassword(accountName, password);
    }

    /**
     * findByName
     *
     * @param accountName accountName
     * @return entity
     */
    public List<Customer> findByAccountName(String accountName) {
        log.debug("Request to findByName Customer : {}", accountName);
        return customerRepository.findByAccountName(accountName);
    }

    /**
     * findByMobile
     *
     * @param mobile mobile
     * @return customer
     */
    public Customer findByMobile(String mobile) {
        log.debug("Request to findByMobile Customer : {}", mobile);
        return customerRepository.findByMobile(mobile);
    }


    /**
     * 根据代理查找客户数量
     *
     * @param agency 代理
     * @return 客户数量
     */
    public int findCountByAgency(String agency) {
        log.debug("equest to findCountByAgency agency : {}", agency);
        return customerRepository.findCountByAgency(agency);
    }
}
